package com.test.exercise.impl;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
/**
 * description: 自己写一个 java.lang.ThreadLocal ,仿制的山寨版
 * @version v1.0
 * @author w
 * @date 2018年12月1日下午4:37:57
 **/
public class MyThreadLocal<T> {
		
	private Map<Thread,T> container = Collections.synchronizedMap(new HashMap<Thread,T>());
	
	protected T initVariable(){
		return null;
	}
	
	public T get(){
		Thread currentThread = Thread.currentThread();
		T value = container.get(currentThread);
		if(null == value && !container.containsKey(currentThread)){
			value = initVariable();
			container.put(currentThread, value);
		}
		return value ;
	}
	
	public void set(T value){
		container.put(Thread.currentThread(), value);
	}
	
	public void remove(){
		container.remove(Thread.currentThread());
	}
}
